import { IGeoFile } from '@/types/geofile'

function useFileHelper() {
    function toByteSize(bytes: number) {
        if (!bytes) return '-'
        if (bytes === 0) return '0 B'
        const k = 1000, // or 1024
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            i = Math.floor(Math.log(bytes) / Math.log(k))
        return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
    }

    function getFileSource(file: IGeoFile) {
        switch (file.data_sources) {
            case 'buy':
                return '购买'
            case 'crawl':
                return '爬虫'
            case 'generate':
                return '生成'
            case 'upload':
                return '上传'
            default:
                return '未知'
        }
    }

    return {
        toByteSize,
        getFileSource,
    }
}

export default useFileHelper
